/* authentication related services */

var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);
//var App = React.createFactory(main.App);

router.post("/create", function(req, res, next){
  var token = req.cookies.access_token;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/propertyBillLines/create',
    body: req.body,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.post("/update", function(req, res, next){
  var token = req.cookies.access_token;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/propertyBillLines/update',
    body: req.body,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);

      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }

    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.post("/updatePay", function(req, res, next){
  var token = req.cookies.access_token;
  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/propertyBillLines/updatePay',
    body: req.body,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

router.get("/delete", function(req, res, next){

  var token = req.cookies.access_token;
  var option = {
    method: 'get',
    uri: config.apiHost+'/api/propertyBillLines/delete?id='+req.query.id,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }

  rp(option)
    .then(function(result) {
      debug(result);
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode
      })
    })
})

module.exports = router;
